package com.pro.controller;

import com.pro.pojo.JsonData;
import com.pro.service.SystemService;
import org.apache.catalina.util.ServerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;

@Controller
@RequestMapping("/system")
public class SystemController{
    @Autowired
    private SystemService systemService;

    //获取mysql的版本
    @RequestMapping("/getSystem")
    @ResponseBody
    public JsonData getSystem(){
        String system = systemService.getSystem();
        return new JsonData(200,"",system);
    }


    //获取服务器地址
    @RequestMapping("/getServerIp")
    @ResponseBody
    public JsonData getServerName(HttpServletRequest request, HttpServletResponse response){
        String serverName = request.getServerName();
        return serverName != null ? JsonData.buildSuccess(serverName) : JsonData.buildError("服务器出错了");
    }

    //获取服务器端口号
    @RequestMapping("/getServerPort")
    @ResponseBody
    public JsonData getServerPort(HttpServletRequest request, HttpServletResponse response){
        int serverPort = request.getServerPort();
        return serverPort != 0 ? JsonData.buildSuccess(serverPort) : JsonData.buildError("服务器出错了");
    }

    //获取服务器名称
    @RequestMapping("/getServerName")
    @ResponseBody
    public JsonData getServer(HttpServletRequest request, HttpServletResponse response){
        String serverNumber = ServerInfo.getServerNumber();
        String serverInfo = ServerInfo.getServerInfo();
        String serverBuilt = ServerInfo.getServerBuilt();
        return serverInfo != null ? JsonData.buildSuccess(serverInfo) : JsonData.buildError("服务器出错了");
    }

    //获取操作系统
    @RequestMapping("/getOperating")
    @ResponseBody
    public JsonData getOperating(HttpServletRequest request, HttpServletResponse response){
        Properties props = System.getProperties();
        String property = props.getProperty("os.name");//操作系统的名称
        return property != null ? JsonData.buildSuccess(property) : JsonData.buildError("服务器出错了");
    }

    //获取运行环境
    @RequestMapping("/getEnvironment")
    @ResponseBody
    public JsonData getEnvironment(HttpServletRequest request, HttpServletResponse response){
        Properties properties = System.getProperties();//所有信息
        System.getProperty("java.runtime.version");
        System.getProperty("java.specification.vendor");
        System.getProperty("java.vendor.url");
        String Environment = System.getProperty("java.runtime.version")+","+System.getProperty("java.specification.vendor")+","+ System.getProperty("java.vendor.url");
        return Environment != null ? JsonData.buildSuccess(Environment) : JsonData.buildError("服务器出错了");
    }

}
